﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

namespace WebApplication1
{
    
    public struct Review
    {
        public int review_id;
        public string section, title, text;
        public int accurate, helpful, easy_to_understand, complete;

    }

    public partial class _Default : System.Web.UI.Page
    {
        #region login section
        private void LoginUser()
        {
            Session["username"] = txtUsername.Text;
            //user.InnerHtml = "Welcome, " + Session["username"] + ".";
            divUser.Visible = false;
            divLoggedUser.Visible = true;
            lblUsername.Text = txtUsername.Text;

            //clear out text boxes
            txtUsername.Text = "";
            txtPassword.Text = "";
            
        }

        protected void lbLogout_Function(object sender, EventArgs e)
        {
            Session["username"] = "";
            divLoggedUser.Visible = false;
            divUser.Visible = true;
        }

        protected void btnSendConfirmation_Click(object sender, EventArgs e)
        {
            LoginUser();
        }
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {

            story.InnerHtml = "";

            SqlConnection myConnection = new SqlConnection("Data Source=tcp:s01.winhost.com;Initial Catalog=DB_2102_main;User ID=DB_2102_main_user;Password=cattleprod;Integrated Security=False;");
            myConnection.Open();

            SqlCommand reviewSelect = new SqlCommand("GetLatestReviewsByCount", myConnection);
            reviewSelect.Parameters.Add(new SqlParameter("@count", 10));
            reviewSelect.CommandType = CommandType.StoredProcedure;
            SqlDataReader reviewReader = reviewSelect.ExecuteReader();

            List<Review> reviews = new List<Review>();
            Review r;

            while(reviewReader.Read()) {
                r = new Review();
                r.section = reviewReader["section_name"].ToString().Trim();
                r.title = reviewReader["title"].ToString().Trim();
                r.text = reviewReader["review_text"].ToString().Trim();
                r.review_id = int.Parse(reviewReader["review_id"].ToString().Trim());

                reviews.Add(r);
            }

            reviewReader.Close();

            if (reviews.Count == 0)
            {
                story.InnerHtml = "No stories available.";
            }
            else
            {

                SqlCommand ratingSelect;

                for (int i = 0; i < reviews.Count; i++)
                {
                    r = reviews[i];

                    ratingSelect = new SqlCommand("GetRatingByReviewid", myConnection);
                    ratingSelect.Parameters.Add(new SqlParameter("@review_id", r.review_id));
                    ratingSelect.CommandType = CommandType.StoredProcedure;
                    SqlDataReader ratingReader = ratingSelect.ExecuteReader();

                    if (!ratingReader.Read())
                    {
                        r.accurate = -1;
                        r.helpful = -1;
                        r.easy_to_understand = -1;
                        r.complete = -1;
                    }
                    else
                    {
                        r.accurate = int.Parse(ratingReader["accurate"].ToString().Trim());
                        r.helpful = int.Parse(ratingReader["helpful"].ToString().Trim());
                        r.easy_to_understand = int.Parse(ratingReader["easy_to_understand"].ToString().Trim());
                        r.complete = int.Parse(ratingReader["complete"].ToString().Trim());
                    }

                    story.InnerHtml += "<tr><td><font size=\"+1\"<b>" + r.section + ":</b>&nbsp;" + r.title + "</font></td></tr>";
                    story.InnerHtml += "<tr><td>" + r.text + "<BR>" + "Accurate: " + r.accurate + " &nbsp;Helpful: " + r.helpful + " &nbsp;Easy To Understand: " + r.easy_to_understand + " &nbsp;Complete: " + r.complete + "<BR>Read the comments. &nbsp;Rate this review.</td></tr>";

                    ratingReader.Close();

                }

            }

        }





        

    }
}
